package com.hqyj.springbootday01.controller;

import com.github.pagehelper.PageInfo;
import com.hqyj.springbootday01.entity.City;
import com.hqyj.springbootday01.service.CityService;
import com.hqyj.springbootday01.vo.Result;
import com.hqyj.springbootday01.vo.Search;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * City Controller
 */
@RestController
@RequestMapping("/api")
public class CityController {

	@Autowired
	private CityService cityService;

	/**
	 * 127.0.0.1/api/city ---- post
	 * {"cityName":"dream","localCityName":"梦想之城","countryId":522}
	 */
	@PostMapping(value = "/city", consumes = "application/json")
	public Result<City> insertCity(@RequestBody City city) {
		return cityService.insertCity(city);
	}

	/**
	 * 127.0.0.1/api/city  ----put
	 * cityName=dream,localCityName=梦想之城,cityId=1890
	 */
	@PutMapping(value = "/city", consumes = "application/x-www-form-urlencoded")
	public Result<City> updateCity(@ModelAttribute City city){
		return cityService.updateCity(city);
	}

	/**
	 * 127.0.0.1/api/city/1895  ----delete
	 */
	@DeleteMapping(value = "/city/{cityId}")
	public Result<City> deleteCity(@PathVariable int cityId){
		return cityService.deleteCityByCityId(cityId);
	}

	/**
	 * 127.0.0.1/api/city/1890   ----get
	 */
	@GetMapping(value = "/city/{cityId}")
	public City getCityByCityId(@PathVariable int cityId){
		return cityService.getCityByCityId(cityId);
	}

	/**
	 * 127.0.0.1/api/cities/522  ----get
	 */
	@GetMapping(value = "/cities/{countryId}")
	public List<City> getCitiesByCountryId(@PathVariable int countryId){
		return cityService.getCitiesByCountryId(countryId);
	}

	/**
	 * 127.0.0.1/api/cities   ----post
	 * {"currentPage":1,"pageSize":10,"sort":"city_id","direction":"desc","keyword":"shan"}
	 */
	@PostMapping(value = "/cities", consumes = "application/json")
	public PageInfo<City> getCitiesBySearch(@RequestBody Search search){
		return cityService.getCitiesBySearch(search);
	}
}
